import React, {MutableRefObject} from "react";
import {Access} from "@@/exports";
import {ModalForm, ProFormInstance, ProFormSelect, ProFormText} from "@ant-design/pro-components";
import {Button, Col, Row, Tooltip} from "antd";
import {PlusOutlined} from "@ant-design/icons";
import {listCategory} from "@/pages/basicdata/product-category/api";
import {listBrand} from "@/pages/basicdata/brand-list/api";
import {FormInstance} from "antd/lib";

interface ISimpleActionForm<T> {
  action?: 'add' | 'edit';
  access?: boolean;
  title?: string;
  initialValues?: T;
  onOpenChange?: (open: boolean) => void;
  onFinish: (values: T) => Promise<boolean>;
  formRef: React.RefObject<ProFormInstance>;
  trigger: React.ReactElement;
  children?: React.ReactNode;
  formWidth?: number;
}

const SimpleActionForm: React.FC<ISimpleActionForm<any>> = <T extends unknown>({
                                                                                 action = 'add',
                                                                                 access = true,
                                                                                 title,
                                                                                 initialValues,
                                                                                 onOpenChange,
                                                                                 onFinish,
                                                                                 formRef,
                                                                                 trigger,
                                                                                 children,
                                                                                 formWidth
                                                                               }: ISimpleActionForm<T>) => {
  return (
    <Access accessible={access}>
      <ModalForm
        title={title}
        initialValues={initialValues as any}
        onOpenChange={onOpenChange}
        onFinish={onFinish}
        formRef={formRef}
        autoFocusFirstInput
        trigger={trigger}
        width={formWidth}
        modalProps={{destroyOnClose: true}}
      >
        {children}
      </ModalForm>
    </Access>
  );
}

export default SimpleActionForm;
